<template>
  <div class="login-panel">
    <h1 class="title">宏源商场后台管理</h1>

    <div class="tabs">
      <!-- v-model: 双向绑定的是el-tab-name中name的属性 -->
      <el-tabs v-model="activeName" type="border-card" stretch @tab-click="tabClick">
        <!-- 账号登陆 -->
        <el-tab-pane name="account">
          <template #label>
            <div class="label">
              <el-icon><User /></el-icon>
              <span class="text">账号登陆</span>
            </div>
          </template>

          <pane-account ref="accountRef" v-model:loading="loading" />
        </el-tab-pane>

        <!-- 手机登陆 -->
        <el-tab-pane label="手机登陆" name="phone">
          <template #label>
            <div class="label">
              <el-icon><Iphone /></el-icon>
              <span class="text">账号登陆</span>
            </div>
          </template>

          <pane-phone ref="panePhoneRef" v-model:loading="loading" />
        </el-tab-pane>
      </el-tabs>
    </div>

    <div class="controls">
      <el-checkbox v-model="isRemPwd" label="记住密码" size="large" />
      <el-link type="primary">忘记密码</el-link>
    </div>

    <el-button class="login-btn" type="primary" size="large" @click="login" :loading="loading"> 立即登录 </el-button>
  </div>
</template>

<script setup lang="ts">
  import { ref, watch } from 'vue'
  import paneAccount from './pane-account.vue'
  import panePhone from './pane-phone.vue'
  import { localCache } from '@/utils/cache'
  import { CACHE_ISREMPWD } from '@/global/constant'

  const activeName = ref<string>('account')
  const isRemPwd = ref<boolean>(localCache.getCache(CACHE_ISREMPWD) ?? false)
  watch(isRemPwd, (newVal) => {
    localCache.setCache(CACHE_ISREMPWD, newVal)
  })
  /*
    typeof paneIAccount获取的是当前组件的类类型，如果需要获取实例类型，
    需要使用到InstanceType(vue组件创建内部使用的class创建，当使用vue组件时，相当于创建了一个实例)

    typeof 组件 可以获取到当前组件实例的类类型，
    InstanceType<typeof 组件> 可以获取当当前组件类的实例类型
  */
  const accountRef = ref<InstanceType<typeof paneAccount>>()
  const panePhoneRef = ref<InstanceType<typeof panePhone>>()
  const loading = ref<boolean>(false)
  function login() {
    if (activeName.value === 'account') {
      accountRef.value?.loginAction(isRemPwd.value)
    } else {
      panePhoneRef.value?.loginAction(isRemPwd.value)
    }
  }

  // tab切换
  function tabClick() {
    loading.value = false
  }
</script>

<style lang="scss" scoped>
  .login-panel {
    width: 330px;
    margin-bottom: 150px;
    .title {
      text-align: center;
      margin-bottom: 15px;
    }

    .tabs {
      .label {
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .text {
        margin-left: 5px;
      }
    }

    .controls {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }

    .login-btn {
      // 使用变量修改css变量修改样式
      // --el-button-size: 50px;
      width: 100%;
    }
  }
</style>
